//
//  NetManager.m
//  MapTest
//
//  Created by JCK on 2017/6/1.
//  Copyright © 2017年 JCK. All rights reserved.
//

#import "NetManager.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"
static NetManager *manger = nil;
static NSString *code;
#import <JavaScriptCore/JavaScriptCore.h>
@implementation NetManager

// 单例
+ (instancetype)shareInstance{
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken, ^{
        if (!manger) {
            manger = [[[self class] alloc] init];
        }
    });
    return manger;
}

- (instancetype)initWith:(RequestState)requet
{
    self = [super init];
    if (self)
    {
        [self loadData:requet];
    }
    return self;
}
- (void)loadData:(RequestState)requet
{
    
    switch (requet) {
        case RequestOfLogin:
        {
            [self login];
        }
            break;
        case RequestOfDrivinglocationsave:
        {
            [self Drivinglocationsave];
        }
            break;
        case RequestOfGettrucklist:
        {
            [self Gettrucklist];
        }
            break;
        case RequestOfGettruckbydriverlist:
        {
            [self Gettruckbydriverlist];
        }
            break;
        case RequestOfUploadimg:
        {
            [self Uploadimg];
        }
            break;
        case RequestOfInitordertrans:
        {
            [self Initordertrans];
        }
            break;
        case RequestOfUpdatetruckposition:
        {
            [self updatetruckposition];
        }
            break;
        case RequestOfUploadUserImg:
        {
            [self UploadUserImg];
        }
            break;
        case RequestOfLoginbythird:
        {
            [self loginbythird];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 通用
+ (void)POST:(NSString *)path Parametes:(NSDictionary *)parameters
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;

    NSString *url = [NSString stringWithFormat:@"%@%@",ServerAddressURL,path];
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSLog(@"%@",responseObject);
         [SVProgressHUD dismiss];
         [[NSNotificationCenter defaultCenter] postNotificationName:path object:nil];
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         NSLog(@"error %ld",error.code);
         [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error]];
     }];
}
#pragma mark - 第三方登录
- (void)loginbythird
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *parameters = @{
                                 @"_appid" : @"101",
                                 @"_version" : @"101",
                                 @"AccountId": _AccountId,
                                 @"AccountType": @"2",
                                 @"NickName": _NickName,
                                 @"HeadPhoto": _HeadPhoto,
                                 @"PlatformType":@"2"
                                 };
    NSString *url = [NSString stringWithFormat:@"%@/systemset/account/loginbythird",ServerAddressURL];
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSLog(@"%@",responseObject);
         [SVProgressHUD dismiss];
         NSInteger enumcode = [[NSString stringWithFormat:@"%@",responseObject[@"enumcode"]] integerValue];
         if (enumcode == 2)
         {
             _loginbythirdUrl = [NSString stringWithFormat:@"%@",responseObject[@"data"]];
             [[NSNotificationCenter defaultCenter] postNotificationName:@"loginbythirdregistered" object:nil];
         }
         else
         {
             code = responseObject[@"data"][@"Token"];
             _userToken = code;
             _UserType = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"UserType"]];
             _UserId = responseObject[@"data"][@"UserId"];
             NSString *isDriverStr = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"IsDriver"]];
             _IsDriver = [isDriverStr intValue];
             [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:responseObject[@"code"]];
//             [[NSNotificationCenter defaultCenter] postNotificationName:@"loginbythird" object:nil];
         }
         
         
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         NSLog(@"%@",error);
          [[NSNotificationCenter defaultCenter] postNotificationName:@"errorlogin" object:[NSString stringWithFormat:@"%ld",error.code]];
     }];
}
#pragma mark - 上传位置
- (void)updatetruckposition
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *parameters = @{
                                 @"_appid" : @"101",
                                 @"_version" : @"101",
                                 @"_code": code,
                                 @"userId": _UserId,
                                 @"Latitude": _Latitude,
                                 @"Longitude": _Longitude,
                                 };
    NSString *url = [NSString stringWithFormat:@"%@/common/truckinfo/updatetruckposition",ServerAddressURL];
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSLog(@"%@",responseObject);
         [SVProgressHUD dismiss];
//         [[NSNotificationCenter defaultCenter] postNotificationName:<##> object:nil];
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         NSLog(@"error %ld",error.code);
//         [SVProgressHUD showErrorWithStatus:error.code];
     }];
}
#pragma mark - 停止鹰眼后反馈
- (void)Initordertrans
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *parameters = @{
                                 @"_appid" : @"101",
                                 @"_version" : @"101",
                                 @"_code": code,
                                 @"Id": _truckId,
                                 @"AppId": @"101"
                                 };
    NSString *url = [NSString stringWithFormat:@"%@/common/drivinglocation/initordertrans",ServerAddressURL];
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSLog(@"%@",responseObject);
         [SVProgressHUD dismiss];
//         [[NSNotificationCenter defaultCenter] postNotificationName:<##> object:nil];
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         NSLog(@"error %ld",error.code);
     }];
}
#pragma mark - 上传头像
- (void)UploadUserImg
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *parameters = @{
                                 @"_appid" : @"101",
                                 @"_version" : @"101",
                                 @"FileData":_FileData,
                                 @"fileName":@"photo.png",
                                 @"AttachmentName":@"photo.png",
                                 @"AttachmentType":@"1",
                                 @"AttachmentFileSize":_AttachmentFileSize,
                                 @"_code" : code
                                 };
    NSString *url = [NSString stringWithFormat:@"%@/common/User/uploadUserImg",ServerAddressURL];
    NSLog(@"%@",_AttachmentFileSize);
    NSLog(@"url = %@",url);
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         // UserId  Account
         NSLog(@"%@",responseObject);
         _AttachmentUrl = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"Account"]];
         NSString *str = [NSString stringWithFormat:@"%@|%@",_AttachmentUrl,responseObject[@"data"][@"UserId"]];
         [SVProgressHUD dismiss];
         
         [[NSNotificationCenter defaultCenter] postNotificationName:@"UploadUserImg" object:str];
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         NSLog(@"error %ld",error.code);
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%ld",error.code]];
     }];
}
#pragma mark - 上传图片
- (void)Uploadimg
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 15;
    NSDictionary *parameters = @{
                                 @"_appid" : @"101",
                                 @"_version" : @"101",
                                 @"FileData":_FileData,
                                 @"fileName":@"photo.png",
                                 @"AttachmentName":@"photo.png",
                                 @"AttachmentType":@"1",
                                 @"AttachmentFileSize":_AttachmentFileSize,
                                 };
    
    NSString *url = [NSString stringWithFormat:@"%@/systemset/uploadimg",ServerAddressURL];
    NSLog(@"%@",parameters);
    NSLog(@"url = %@",url);
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSLog(@"%@",responseObject);
         _AttachmentUrl = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"AttachmentUrl"]];
         NSString *str = [NSString stringWithFormat:@"%@|%@|%@",_AttachmentUrl,responseObject[@"data"][@"Id"],responseObject[@"code"]];
         [SVProgressHUD dismiss];
         
         [[NSNotificationCenter defaultCenter] postNotificationName:@"uploadimg" object:str];
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         NSLog(@"error %ld",error.code);
         //         [SVProgressHUD showErrorWithStatus:error.code];
     }];
}
#pragma mark - 首页获取车辆信息（车主）
- (void)Gettruckbydriverlist
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *parameters = @{
                                 @"_appid" : @"101",
                                 @"_version" : @"101",
                                 @"_code": code,
                                 @"Latitude": _Latitude,
                                 @"Longitude": _Longitude,
                                 };
    
    NSString *url = [NSString stringWithFormat:@"%@/common/truckinfo/gettruckbydriverlist",ServerAddressURL];
    NSLog(@" parameters -%@ | url - %@",parameters,url);
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSLog(@"%@",responseObject);
         if ([responseObject[@"msg"] isEqualToString:@"success"])
         {
             _carInfo = nil;
             _carInfo = responseObject[@"data"];
             [SVProgressHUD dismiss];
             [[NSNotificationCenter defaultCenter] postNotificationName:@"gettrucklist" object:nil];
         }
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         NSLog(@"error %ld",error.code);
         [SVProgressHUD showErrorWithStatus:@"网络请求错误"];
     }];
}

#pragma mark - 首页获取车辆信息（承建商）
- (void)Gettrucklist
{
    NSLog(@"首页获取车辆信息（承建商）");
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *parameters = @{
                                 @"_appid" : @"101",
                                 @"_version" : @"101",
                                 @"_code": code,
                                 @"sType":@"1",
                                 @"Distance":@"0",
                                 @"Latitude": _Latitude,
                                 @"Longitude": _Longitude,
                                 };
    
    NSString *url = [NSString stringWithFormat:@"%@/common/truckinfo/gettrucklist",ServerAddressURL];
    NSLog(@" parameters -%@ | url - %@",parameters,url);
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSLog(@"%@",responseObject);
         if ([responseObject[@"msg"] isEqualToString:@"success"])
         {
             _carInfo = nil;
             _carInfo = responseObject[@"data"];
             [SVProgressHUD dismiss];
             [[NSNotificationCenter defaultCenter] postNotificationName:@"gettrucklist" object:nil];
         }
         
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         NSLog(@"error %ld",error.code);
         [SVProgressHUD showErrorWithStatus:@"网络请求错误"];
     }];
}
#pragma mark - 汽车行驶轨迹保存（新增，修改）
- (void)Drivinglocationsave
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *parameters = @{
                                 @"_appid" : @"101",
                                 @"_version" : @"101",
                                 @"_code": code,
                                 @"TruckId": _truckId, //TruckId
                                 @"DetailId": _DetailId,// id
                                 @"Latitude": _Latitude,
                                 @"Longitude": _Longitude,
                                 
                                 };
    NSLog(@"%@",parameters);
    NSString *url = [NSString stringWithFormat:@"%@/common/drivinglocation/drivinglocationsave",ServerAddressURL];
    NSLog(@"%@",url);
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSLog(@"%@",responseObject);
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         NSLog(@"error %ld",error.code);
         //         [SVProgressHUD showErrorWithStatus:error.code];
     }];
}
- (void)login
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSDictionary *parameters = @{
                                 @"_appid" : @"101",
                                 @"_version" : @"101",
                                 @"Account": self.userName,
                                 @"Password": self.userPWD,
                                 @"PlatformType":@"2"
                                 };
    NSString *url = [NSString stringWithFormat:@"%@/systemset/account/login",ServerAddressURL];
    NSLog(@"%@ %@",parameters,url);
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSLog(@"%@",responseObject);
         if ([responseObject[@"msg"] isEqualToString:@"success"])
         {
             code = responseObject[@"data"][@"Token"];
             _userToken = code;
             _UserType = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"UserType"]];
             _UserId = responseObject[@"data"][@"UserId"];
             NSString *isDriverStr = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"IsDriver"]];
             _IsDriver = [isDriverStr intValue];
             [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:responseObject[@"code"]];
         }
         else
         {
             [SVProgressHUD showImage:[UIImage imageNamed:@"errorlogin"] status:responseObject[@"msg"]];
             [[NSNotificationCenter defaultCenter] postNotificationName:@"errorlogin" object:responseObject[@"code"]];
             [self performSelector:@selector(dismiss) withObject:nil afterDelay:1];
             
         }
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         NSLog(@"error %@",error);
         {
             
//             [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%ld",error.code]];
             [[NSNotificationCenter defaultCenter] postNotificationName:@"errorlogin" object:[NSString stringWithFormat:@"%ld",error.code]];

         }
         
     }];
}


- (void)dismiss
{
    [SVProgressHUD dismiss];
}
@end
